<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 2017/1/6
 * Time: 19:53
 */

namespace app\src\task\logic;


use app\src\base\logic\BaseLogic;
use app\src\buyorder\logic\BuyOrderLogic;
use app\src\goods\logic\ProductSkuLogic;
use app\src\product\logic\ProductclassifyLogic;
use app\src\task\model\RoleTask;
use app\src\user\logic\UserMemberLogic;
use app\src\wallet\logic\WalletLogic;
use app\weixin\controller\Income;
use think\Db;

class RoleTaskLogic extends BaseLogic{
    public function _init()
    {
        $this->setModel(new RoleTask());
    }

    //增加定时任务
    //$uid,$cash_point,$need_point
    /*
     * $uid,$from_uid=0,$type,$income,$stock,$group_id=0
     */
    public function add_task($uid,$from_uid=0,$type,$income,$stock,$group_id=0,$pid='0'){

        if(empty($uid)||empty($type)||empty($income)) return false;

        $is_task=$this->getInfo(['from_uid'=>$uid,'type'=>['in','1,3,4,5'],'status'=>'0']);

        if($is_task['status']&&!empty($is_task['info'])){
            $time=strtotime('now')+172800;
        }else{
            $time=strtotime('now');
        }
        $order=empty(session('order'))?0:session('order');
        $entity=[
            'uid'=>$uid,
            'from_uid'=>$from_uid,
            'type'=>$type,
            'income'=>$income,
            'stock'=>$stock,
            'group_id'=>$group_id,
            'status'=>'0',
            'create_time'=>$time,
            'order'=>$order,
            'pid'=>$pid,
        ];

        $add=$this->add($entity,'');


        if(!$add['status']) return false;
        return true;
    }




    //处理定时任务
    public function deal_task(){
        //取出任务

        $map['status']=0;
        $time=strtotime('-2 days');
        $map['create_time']=['ELT',$time];

        $tasks=$this->queryNoPaging($map);

        if(!$tasks['status']) exit;
        if(empty($tasks['info'])) exit;

        foreach($tasks['info'] as $k=>$v){
            session('order',$v['order']);

            $buy_order=(new BuyOrderLogic())->getInfo(['id'=>$v['order']]);
            $pid=$buy_order['info']['pid'];
            session('session_pid',$pid);
            if($v['type']==1){
                //如果是升级任务处理
                $id=$v['id'];
                $uid=$v['uid'];
                $from_uid=$v['from_uid'];
                $income=$v['income'];
                $stock=$v['stock'];
                $group_id=$v['group_id'];
                $order=$v['order'];
                $deal=$this->deal_level($id,$uid,$from_uid,$income,$stock,$group_id);

            }elseif($v['type']==2){
                //如果是存货任务处理
                $id=$v['id'];
                $uid=$v['uid'];
                $income=$v['income'];
                $stock=$v['stock'];
                $order=$v['order'];
                $pid=$v['pid'];

                $this->deal_stock($id,$uid,$income,$stock,$pid);

            }elseif($v['type']==3){
                //发起人为精英，承接人为精英,则成同级，结算同级收益,结算上级收益
                $id=$v['id'];
                $uid=$v['uid'];
                $from_uid=$v['from_uid'];
                $income=$v['income'];
                $stock=$v['stock'];
                $group_id=$v['group_id'];
                $order=$v['order'];
                $pid=$v['pid'];
                $deal=$this->deal_elite_elite($id,$uid,$from_uid,$income,$stock,$group_id,$pid);
            }elseif($v['type']==4){
                //发起人为精英，承接人为领袖,则成上下级，结算上下级收益,结算同级收益
                $id=$v['id'];
                $uid=$v['uid'];
                $from_uid=$v['from_uid'];
                $income=$v['income'];
                $stock=$v['stock'];
                $group_id=$v['group_id'];
                $order=$v['order'];
                $pid=$v['pid'];
                $deal=$this->deal_elite_leader($id,$uid,$from_uid,$income,$stock,$group_id,$pid);
            }elseif($v['type']==5){
                //发起人为领袖，承接人为领袖,则成同级，结算同级收益
                $id=$v['id'];
                $uid=$v['uid'];
                $from_uid=$v['from_uid'];
                $income=$v['income'];
                $stock=$v['stock'];
                $group_id=$v['group_id'];
                $order=$v['order'];
                $pid=$v['pid'];
                $deal=$this->deal_leader_leader($id,$uid,$from_uid,$income,$stock,$group_id,$pid);

            }

        }
    }


    /*
     * 存货任务处理：下级补货我的货物不足，下级升级我的货物不足
     * 判断是否存货库存充足，如果充足则做减库存加提现
     */
    public function deal_stock($id,$uid,$income,$stock,$pid){


        //查询此人的钱包
        $object=(new WalletLogic())->getInfo(['uid'=>$uid]);
        if(!$object['status']) exit;
        if(empty($object['info'])) exit;

        $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$pid]);
        $group_id=$classify['info']['pid_type'];

        $is_enough=(new Income())->is_enough($uid,$stock,'','',$pid,$classify['info']['pid_type']);

        if(!$is_enough) {
        //if($object['info']['stock_points']<$stock){
            //若未完成存货任务，不做处理
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
        }else{
            //若完成任务
            //减库存
            $note='下级补充货减库存积分';;$datatree=config('datatree.after_buy_minus');
            //$wallet_deal=(new Income())->wallet_update($note,$stock,'0','1',$uid,$datatree);
            $wallet_deal=(new Income())->mius_goods_stock($uid,$pid,$group_id,$stock,'','',$note,$datatree);
            //加提现
            $note='下级补充货加提现积分';$datatree=config('datatree.after_buy_plus');
            $wallet_deal=(new Income())->wallet_update($note,$income,'1','0',$uid,$datatree);
//            //处理任务
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
        }
    }


    /*
     * 升级任务处理
     * 判断等级是否充足，库存是否充足
     *
     */
    public function deal_level($id,$uid,$from_uid,$income,$stock,$group_id){
        //查询此人的等级
        $people=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
        if(!$people['status']||empty($people['info'])) exit;
        if($people['info']['group_id']=$group_id){
            //首先确认等级足够
            //查询此人的钱包
            $object=(new WalletLogic())->getInfo(['uid'=>$uid]);
            if(!$object['status']) exit;
            if(empty($object['info'])) exit;

//            $is_enough=(new Income())->is_enough($uid,$income,$classify['info']['count'],$upper_price,$pid);
//            if(!$is_enough) {
            if($object['info']['stock_points']<$stock){
                //若库存不足，不做处理
                $model=$this->getModel();
                $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
            }else{
                //减库存
                $note='下级升级减库存积分';;$datatree=config('datatree.after_become_elite_minus');
                $wallet_deal=(new Income())->wallet_update($note,$stock,'0','1',$uid,$datatree);
                //加提现
                $note='下级升级加提现积分';$datatree=config('datatree.after_become_elite_plus');
                $wallet_deal=(new Income())->wallet_update($note,$income,'1','0',$uid,$datatree);
                //处理任务
                $model=$this->getModel();
                $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
            }
        }else{
            //若未完成升级任务，不做处理
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
        }
        return true;
    }



    /*
    * 升级任务处理
    * 判断等级是否充足，库存是否充足
    *发起人为精英，承接人为精英,则成同级，结算同级收益,结算上级收益
    */
    public function deal_elite_elite($id,$uid,$from_uid,$income,$stock,$group_id,$pid){
        //查询此人的等级
        $people=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
        if(!$people['status']||empty($people['info'])) exit;

        if($people['info']['group_id']==$group_id){
            //首先确认等级相符

            //结算同级收益
            $pid='';
            if(!empty(session('session_pid')))$pid=session('session_pid');
            $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$pid]);

            $group_pid=$classify['info']['pid_type'];
            $type=$classify['info']['type'];
            $user_member=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
            $role=$user_member['info']['group_id'];
            $product=(new ProductSkuLogic())->getInfo(['product_id'=>$group_pid]);
            if($type==1){
                $price=$product['info']['price']*0.95;
            }else{
                if($role==4) $price=$product['info']['price']*0.95;
                if($role==1) $price=$classify['info']['angel_price'];
                if($role==2) $price=$classify['info']['elite_price'];
                if($role==3) $price=$classify['info']['leader_price'];
            }

            $count=floor($income/$price);


            $ahead=(new Income())->ahead_income($from_uid,$count,'6','4',$pid);
            $upper_count=$ahead['status']?$ahead['count']:0;
            $me=(new UserMemberLogic())->getInfo(['uid'=>$from_uid]);
            $upper_id=$me['info']['goods_uid'];
            $upper=(new UserMemberLogic())->getInfo(['uid'=>$upper_id]);

            if($upper['info']['type']!=1){
                $object=(new WalletLogic())->getInfo(['uid'=>$upper_id]);
                //查询上级的钱包
                if(!$object['status']) exit;
                if(empty($object['info'])) exit;

                $is_enough=(new Income())->is_enough($uid,$income,$count,$price,$classify['info']['pid_type']);
                if($is_enough) {
                //if($object['info']['stock_points']<$stock){
                    //若库存不足，不做处理
                    $model=$this->getModel();
                    $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
                }else{
                    //TODO
                    //先写死
                    $stock_minus=8467;
                    $cash_plus=9980-$upper_count;
                    //减库存
                    $note='下级升级减库存积分';;$datatree=config('datatree.after_become_elite_minus');
                    $wallet_deal=(new Income())->mius_goods_stock($upper_id,$pid,$group_pid,$income,$count,$price,$note,$datatree);
                    //$wallet_deal=(new Income())->wallet_update($note,$stock_minus,'0','1',$upper_id,$datatree);
                    //加提现
                    $note='下级升级加提现积分';$datatree=config('datatree.after_become_elite_plus');
                    $wallet_deal=(new Income())->wallet_update($note,$cash_plus,'1','0',$upper_id,$datatree);
                    //处理任务
                    $model=$this->getModel();
                    $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
                }
            }else{
                $model=$this->getModel();
                $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
            }
        }else{
            //若未完成升级任务，不做处理
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
        }
        return true;
    }



    /*
   * 升级任务处理
   * 判断等级是否充足，库存是否充足
   *发起人为精英，承接人为领袖,则成上下级，,结算上级收益，结算同级收益
   */
    public function deal_elite_leader($id,$uid,$from_uid,$income,$stock,$group_id,$pid){

        //查询此人的等级
        $people=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
        if(!$people['status']||empty($people['info'])) exit;

        if($people['info']['group_id']=$group_id){

            //首先确认等级相符
            //结算同级收益
            $pid='';
            if(!empty(session('session_pid')))$pid=session('session_pid');
            $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$pid]);

            $group_pid=$classify['info']['pid_type'];
            $type=$classify['info']['type'];
            $user_member=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
            $role=$user_member['info']['group_id'];
            $product=(new ProductSkuLogic())->getInfo(['product_id'=>$group_pid]);
            if($type==1){
                $price=$product['info']['price']*0.95;
            }else{
                if($role==4) $price=$product['info']['price']*0.95;
                if($role==1) $price=$classify['info']['angel_price'];
                if($role==2) $price=$classify['info']['elite_price'];
                if($role==3) $price=$classify['info']['leader_price'];
            }

            $count=floor($income/$price);


            $ahead=(new Income())->ahead_income($from_uid,$count,'6','4',$pid);
            $upper_count=$ahead['status']?$ahead['count']:0;
            $me=(new UserMemberLogic())->getInfo(['uid'=>$from_uid]);
            $upper_id=$me['info']['goods_uid'];
            $upper=(new UserMemberLogic())->getInfo(['uid'=>$upper_id]);

               if($upper['info']['type']!=1){
                $object=(new WalletLogic())->getInfo(['uid'=>$upper_id]);
                //查询上级的钱包
                if(!$object['status']) exit;
                if(empty($object['info'])) exit;

                $is_enough=(new Income())->is_enough($uid,$income,$count,$price,$group_pid);

                if(!$is_enough) {
                //if($object['info']['stock_points']<$stock){
                    //若库存不足，不做处理
                    $model=$this->getModel();
                    $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
                }else{
                    //TODO
                    //先写死
                    $stock_minus=8467;
                    $cash_plus=9980-$upper_count;
                    //减库存
                    $note='下级升级减库存积分';;$datatree=config('datatree.after_become_elite_minus');
                    $wallet_deal=(new Income())->mius_goods_stock($upper_id,$pid,$group_pid,$income,$count,$price,$note,$datatree);
                    //$wallet_deal=(new Income())->wallet_update($note,$stock_minus,'0','1',$upper_id,$datatree);
                    //加提现
                    $note='下级升级加提现积分';$datatree=config('datatree.after_become_elite_plus');
                    $wallet_deal=(new Income())->wallet_update($note,$cash_plus,'1','0',$upper_id,$datatree);
                    //处理任务
                    $model=$this->getModel();
                    $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
                }
            }else{
                   $model=$this->getModel();
                   $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
               }
        }else{
            //若未完成升级任务，不做处理
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);
        }
        return true;
    }




    /*
  * 升级任务处理
  * 判断等级是否充足，库存是否充足
  *发起人为领袖，承接人为领袖,则成同级，，结算同级收益
  */
    public function deal_leader_leader($id,$uid,$from_uid,$income,$stock,$group_id,$pid){
        //查询此人的等级

            //首先确认等级相符
            //结算同级收益
        $pid='';
        if(!empty(session('session_pid')))$pid=session('session_pid');
        $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$pid]);

        $group_pid=$classify['info']['pid_type'];
        $type=$classify['info']['type'];
        $user_member=(new UserMemberLogic())->getInfo(['uid'=>$uid]);
        $role=$user_member['info']['group_id'];
        $product=(new ProductSkuLogic())->getInfo(['product_id'=>$group_pid]);
        if($type==1){
            $price=$product['info']['price']*0.95;
        }else{
            if($role==4) $price=$product['info']['price']*0.95;
            if($role==1) $price=$classify['info']['angel_price'];
            if($role==2) $price=$classify['info']['elite_price'];
            if($role==3) $price=$classify['info']['leader_price'];
        }

        $count=floor($income/$price);

            $ahead=(new Income())->ahead_income($from_uid,$count,'6','4',$pid);

            //若未完成升级任务，不做处理
            $model=$this->getModel();
            $deal=$model->where(['id'=>$id])->update(['status'=>'1']);

        return true;
    }


}